package com.dinggou.controller;

import com.dinggou.common.ResponseCode;
import com.dinggou.common.ServerResponse;
import com.dinggou.entity.Jc;
import com.dinggou.entity.JcWithBLOBs;
import com.dinggou.service.JcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/jc")
public class JController {

    @Resource(name="jcServiceImpl")
    @Autowired
    private JcService jcService;

    @RequestMapping("/delete")
    @ResponseBody
    public ServerResponse deleteByPrimaryKey(Integer id){
        if(jcService.deleteByPrimaryKey(id)>0){
            return ServerResponse.createBySuccess(1,"删除管部门单位成功");
        }else{
            return ServerResponse.createByErrorCodeMessage(ResponseCode.ERROR.getCode(),"删除数据失败");
        }
    }

    @RequestMapping("/insert")
    @ResponseBody
    public ServerResponse insert(JcWithBLOBs record){

        Jc jc=new Jc();
        jc.setName(record.getName());
        jc.setIsbn(record.getIsbn());
        jc.setAuthor(record.getAuthor());
        jc.setPublisherId(record.getPublisherId());
        jc.setPrice(record.getPrice());
        jc.setPubdate(record.getPubdate());
        jc.setCategoryId(record.getCategoryId());
        jc.setMajorText(record.getMajorText());
        jc.setClassseId(record.getClassseId());
        jc.setClassseText(record.getClassseText());
        jc.setLevelsId(record.getLevelsId());
        jc.setCourseId(record.getCourseId());
        jc.setTeacherUse(record.getTeacherUse());
        jc.setYesorno(record.getYesorno());
        jc.setSdDatetime(record.getSdDatetime());
        jc.setSdTeacher(record.getSdTeacher());

        if(jcService.insert((JcWithBLOBs) jc)>0){
            return ServerResponse.createBySuccess("添加数据成功",jc);
        }else{
            return ServerResponse.createByError();
        }
    }

    @RequestMapping("/insertSelective")
    @ResponseBody
    public ServerResponse insertSelective(JcWithBLOBs record){

        Jc jc=new Jc();
        jc.setName(record.getName());
        jc.setIsbn(record.getIsbn());
        jc.setAuthor(record.getAuthor());
        jc.setPublisherId(record.getPublisherId());
        jc.setPrice(record.getPrice());
        jc.setPubdate(record.getPubdate());
        jc.setCategoryId(record.getCategoryId());
        jc.setMajorText(record.getMajorText());
        jc.setClassseId(record.getClassseId());
        jc.setClassseText(record.getClassseText());
        jc.setLevelsId(record.getLevelsId());
        jc.setCourseId(record.getCourseId());
        jc.setTeacherUse(record.getTeacherUse());
        jc.setYesorno(record.getYesorno());
        jc.setSdDatetime(record.getSdDatetime());
        jc.setSdTeacher(record.getSdTeacher());

        if(jcService.insert((JcWithBLOBs) jc)>0){
            return ServerResponse.createBySuccess("添加数据成功",jc);
        }else{
            return ServerResponse.createByError();
        }
    }

    @RequestMapping("/selectByPrimaryKey")
    @ResponseBody
    public ServerResponse selectByPrimaryKey(Integer id, HttpServletRequest request, HttpServletResponse response) throws IOException {

        Jc jc = jcService.selectByPrimaryKey(id);

        if(jc != null){
            return ServerResponse.createBySuccess(1,jc);
        }else{
            return ServerResponse.createByErrorMessage("找不到管理员");
        }
    }


    @RequestMapping("/listall")
    @ResponseBody
    public ServerResponse listall(HttpServletRequest request, HttpServletResponse response){
        List<Jc> jcs=jcService.selectAll();
        if(jcs.size()>0){
            return ServerResponse.createBySuccess(jcs.size(),jcs);
        }else{
            return ServerResponse.createByErrorMessage("找不到信息");
        }
    }


    @RequestMapping("/updateByPrimaryKeySelective")
    @ResponseBody
    public ServerResponse updateByPrimaryKeySelective(JcWithBLOBs record) {
        Jc jc=new Jc();
        jc.setId(record.getId());
        jc.setName(record.getName());
        jc.setIsbn(record.getIsbn());
        jc.setAuthor(record.getAuthor());
        jc.setPublisherId(record.getPublisherId());
        jc.setPrice(record.getPrice());
        jc.setPubdate(record.getPubdate());
        jc.setCategoryId(record.getCategoryId());
        jc.setMajorText(record.getMajorText());
        jc.setClassseId(record.getClassseId());
        jc.setClassseText(record.getClassseText());
        jc.setLevelsId(record.getLevelsId());
        jc.setCourseId(record.getCourseId());
        jc.setTeacherUse(record.getTeacherUse());
        jc.setYesorno(record.getYesorno());
        jc.setSdDatetime(record.getSdDatetime());
        jc.setSdTeacher(record.getSdTeacher());


        if(jcService.updateByPrimaryKeySelective((JcWithBLOBs) jc)>0){
            return ServerResponse.createBySuccess("更新管理数据成功",jc);
        }else{
            return ServerResponse.createByErrorMessage("更新管理数据失败");
        }

    }

    @RequestMapping("/updateByPrimaryKeyWithBLOBs")
    @ResponseBody
    public ServerResponse updateByPrimaryKeyWithBLOBs(JcWithBLOBs record){
        Jc jc=new Jc();
        jc.setId(record.getId());
        jc.setName(record.getName());
        jc.setIsbn(record.getIsbn());
        jc.setAuthor(record.getAuthor());
        jc.setPublisherId(record.getPublisherId());
        jc.setPrice(record.getPrice());
        jc.setPubdate(record.getPubdate());
        jc.setCategoryId(record.getCategoryId());
        jc.setMajorText(record.getMajorText());
        jc.setClassseId(record.getClassseId());
        jc.setClassseText(record.getClassseText());
        jc.setLevelsId(record.getLevelsId());
        jc.setCourseId(record.getCourseId());
        jc.setTeacherUse(record.getTeacherUse());
        jc.setYesorno(record.getYesorno());
        jc.setSdDatetime(record.getSdDatetime());
        jc.setSdTeacher(record.getSdTeacher());


        if(jcService.updateByPrimaryKeySelective((JcWithBLOBs) jc)>0){
            return ServerResponse.createBySuccess("更新管理数据成功",jc);
        }else{
            return ServerResponse.createByErrorMessage("更新管理数据失败");
        }

    }

    @RequestMapping("/updateByPrimaryKey")
    @ResponseBody
    public ServerResponse updateByPrimaryKey(Jc record){

        Jc jc=new Jc();
        jc.setId(record.getId());
        jc.setName(record.getName());
        jc.setIsbn(record.getIsbn());
        jc.setAuthor(record.getAuthor());
        jc.setPublisherId(record.getPublisherId());
        jc.setPrice(record.getPrice());
        jc.setPubdate(record.getPubdate());
        jc.setCategoryId(record.getCategoryId());
        jc.setMajorText(record.getMajorText());
        jc.setClassseId(record.getClassseId());
        jc.setClassseText(record.getClassseText());
        jc.setLevelsId(record.getLevelsId());
        jc.setCourseId(record.getCourseId());
        jc.setTeacherUse(record.getTeacherUse());
        jc.setYesorno(record.getYesorno());
        jc.setSdDatetime(record.getSdDatetime());
        jc.setSdTeacher(record.getSdTeacher());

        if(jcService.updateByPrimaryKeySelective((JcWithBLOBs) jc)>0){
            return ServerResponse.createBySuccess("更新管理数据成功",jc);
        }else{
            return ServerResponse.createByErrorMessage("更新管理数据失败");
        }

    }
}